package board;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

public class BoardApplet extends Applet {
	// Define some parameter:
	GoBoard board;
	int width;
	int height;
	int type;
	Point anchorBoard = new Point(); // anchor of Board
	Point anchorGrid = new Point(); // anchor of Board Grid.
	// color
	public static final int _BLACK = 0;
	public static final int _WHITE = 1;
	int color = _BLACK;

	public void init() {
		this.configureBoard(9);
	}

	public void configureBoard(int type) {
		this.width = this.getSize().width;
		this.height = this.getSize().height;
		int boardsize = ((width < height) ? width : height) * 80 / 100;
		board = new GoBoard(type, boardsize);
		// set anchor
		anchorBoard.setLocation(5, 5);
		anchorGrid.setLocation(anchorBoard.x + board.getMargin(), anchorBoard.y
				+ board.getMargin());
	}

	public void doTest() {
		this.board.putWhiteStone(1, 2);
		this.board.putWhiteStone(1, 3);
		this.board.putWhiteStone(2, 6);
		this.board.putBlackStone(2, 3);
	}

	public void paint(Graphics g) {
		drawBoard(g);
		drawPieces(g);
		drawLastMove(g);
	}

	private void drawLastMove(Graphics g) {
		GoPosition pos = this.board.getLastMove();
		if(pos == null)
			return;
		int cell = this.board.getCellSize();
		int x = this.anchorGrid.x + pos.getX() * cell;
		int y = this.anchorGrid.y + pos.getY() * cell;
		int piece = this.board.getPieceSize() + 1;
		g.setColor(Color.yellow);
		g.drawOval(x - piece, y - piece, 2 * piece, 2 * piece);
		g.setColor(Color.black);
	}

	public void drawBoard(Graphics g) {
		// begin from board anchor to draw board.
		// draw background
		g.setColor(Color.lightGray);
		g.fillRect(0, 0, this.width, this.height);
		g.setColor(Color.black);
		// draw board outline first;
		Point base = this.anchorBoard;
		g.drawRect(base.x, base.y, this.board.getBoardSize(),
				this.board.getBoardSize());
		// g.drawRect(this.anchorBoard.x, this.anchorBoard.y,
		// this.board.getBoardSize(), this.board.getBoardSize());
		// draw board grid
		base = this.anchorGrid;
		// System.out.println(base.x + " ... " + base.y);
		int cell = this.board.getCellSize();
		int width = cell * (this.board.type - 1);
		for (int i = 0; i < this.board.type; i++) {
			g.drawLine(base.x, base.y + i * cell, base.x + width, base.y + i
					* cell);
		}
		for (int i = 0; i < this.board.type; i++) {
			g.drawLine(base.x + i * cell, base.y, base.x + i * cell, base.y
					+ width);
		}
	}

	private void drawPieces(Graphics g) {
		for (int i = 0; i < this.board.getBoardType(); i++) {
			for (int j = 0; j < this.board.getBoardType(); j++) {
				GoPosition pos = this.board.getPosition(i, j);
				if (!pos.isEmpty()) {
					drawPiece(g, pos);
				}
			}
		}
	}

	private void drawPiece(Graphics g, GoPosition pos) {
		int cell = this.board.getCellSize();
		int x = this.anchorGrid.x + pos.getX() * cell;
		int y = this.anchorGrid.y + pos.getY() * cell;
		if (pos.isBlack()) {
			drawBlackStone(g, x, y);
		} else if (pos.isWhite()) {
			drawWhiteStone(g, x, y);
		}
	}

	private void drawBlackStone(Graphics g, int x, int y) {
		Color co = g.getColor();
		g.setColor(Color.red);
		int piece = this.board.getPieceSize();
		// g.drawOval(x - piece, y - piece, 2 * piece, 2 * piece);
		g.fillOval(x - piece, y - piece, 2 * piece, 2 * piece);
		g.setColor(co);
	}

	private void drawWhiteStone(Graphics g, int x, int y) {
		Color co = g.getColor();
		g.setColor(Color.blue);
		int piece = this.board.getPieceSize();
		// g.drawOval(x - piece, y - piece, 2 * piece, 2 * piece);
		g.fillOval(x - piece, y - piece, 2 * piece, 2 * piece);
		g.setColor(co);
	}
}
